home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_05 / adams / tempvec.cpp < prev    next >
C/C++ Source or Header  |  1994-03-02  |  5KB  |  112 lines

  1.  
  2. Listing 3.  Vector and TempVector with dynamic VES 
  3. class TempVector;
  4. class Vector             // Vector class definition
  5. {    unsigned lng;            // Vector Length
  6.      double *ves;             // Vector Element Space
  7.  public:
  8.      friend TempVector;
  9.      Vector ()                     // Default Constructor
  10.      {    lng = 0;                 // No VES
  11.           ves = NULL; }
  12.      Vector (const unsigned ln)    // New Vector Constructor
  13.      {    lng = ln;                // Set length
  14.           ves = new double [ln]; } // Create new VES
  15.      Vector (const Vector& v) // Copy Constructor
  16.      {    lng = v.lng;             // Set length
  17.           ves = new double [lng];       // Create new VES
  18.           memcpy (ves, v.ves, lng * sizeof(double)); }
  19.      Vector (TempVector&);         // Typecast "Copy" Constructor
  20.      ~Vector () { if (ves != NULL) delete ves; } // Destructor
  21.      double& operator()(const int coordinate) const
  22.      {    // Address elements by coordinate
  23.           return ves[coordinate]; }
  24.      unsigned length() const { return lng; } // Report Vector length
  25.      Vector& operator= (const Vector& v);    // Assignment operator
  26.      Vector& operator= (TempVector& v);      // Assignement operator
  27.      TempVector operator+ (const Vector&) const;  // Vector Addition
  28.      TempVector operator+ (const TempVector&) const;   // Vector Addition
  29.      friend TempVector unit (const Vector&); // Unitize Vector
  30. };
  31. class TempVector              // Temporary Vector class definition
  32. {    unsigned lng;            // Vector Length
  33.      double *ves;             // Vector Element Space
  34.    friend Vector;
  35.      TempVector ()            // Default Constructor
  36.      {    lng = 0;                 // No VES
  37.           ves = NULL; }
  38.      TempVector (const unsigned ln) // New TempVector Constructor
  39.      {    lng = ln;                // Set length
  40.           ves = new double [ln]; } // Allocate new VES
  41.      TempVector (TempVector& v)    // Copy Constructor
  42.      {    lng = v.lng;             // Set length
  43.           ves = v.ves;             // Seize VES 
  44.           v.ves = NULL; }
  45.      unsigned length() const { return lng; } // Report Vector length
  46.   public:
  47.      ~TempVector () { if (ves != NULL) delete ves; } // Destructor
  48.      double& operator()(const int coordinate) const
  49.      {    // Address elements by coordinate
  50.           return ves[coordinate]; }
  51.      friend TempVector unit (const Vector&)  // Unitize Vector
  52.      friend TempVector unit (const TempVector&); // Unitize Vector
  53.      // No assignment operator
  54.      friend Vector::Vector (TempVector&);
  55.      TempVector operator+ (const Vector&) const; // Vector Addition
  56.      TempVector operator+ (const TempVector&) const; // Vector Addition
  57.      friend TempVector Vector::operator+ (const TempVector&);
  58. };
  59. Vector::Vector (TempVector& v)
  60. {    // Typecast "Copy" Constructor from TempVector to Vector
  61.      ves = v.ves;             // Seize VES
  62.      v.ves = NULL;
  63.      lng = v.lng; }           // Set Vector length
  64.  
  65. Vector& Vector::operator= (const Vector& v)
  66. {    // Assignment operator
  67.      if (ves != v.ves) {      // Protect against self-reference
  68.           if (ves != NULL) delete ves;  // Delete VES, if any
  69.           lng = v.lng;             // Set Vector length
  70.           ves = new double [lng];       // Allocate new VES & copy
  71.           memcpy (ves, v.ves, lng * sizeof(double)); }
  72.      return *this; }
  73. Vector& Vector::operator= (TempVector& v)
  74. {    // Assignment operator
  75.      // No danger of self-reference
  76.      if (ves != NULL) delete ves;  // Delete VES, if any
  77.      lng = v.lng;             // Set Vector length
  78.      ves = v.ves;             // Seize VES
  79.      v.ves = NULL;
  80.      return *this; }
  81.  
  82. TempVector Vector::operator+ (const Vector& v) const
  83. {    // Vector addition
  84.      if (v.lng != lng) exit(1);    // Ensure conformal addition
  85.      TempVector u (lng);      // Create same size Vector
  86.      for (int i = 0; i < lng; i++) // Carry out addition
  87.           u.ves[i] = ves[i] + v.ves[i];
  88.      return u; }
  89. TempVector Vector::operator+ (const TempVector& v) const
  90. {    // Vector addition
  91.      if (v.lng != lng) exit(1);    // Ensure conformal addition
  92.      TempVector u (lng);      // Create same size Vector
  93.      for (int i = 0; i < lng; i++) // Carry out addition
  94.           u.ves[i] = ves[i] + v.ves[i];
  95.      return u; }
  96. TempVector TempVector::operator+ (const Vector& v) const
  97. {    // Vector addition
  98.      if (v.lng != lng) exit(1);    // Ensure conformal addition
  99.      TempVector u (lng);      // Create same size Vector
  100.      for (int i = 0; i < lng; i++) // Carry out addition
  101.           u.ves[i] = ves[i] + v.ves[i];
  102.      return u; }
  103. TempVector TempVector::operator+ (const TempVector& v) const
  104. {    // Vector addition
  105.      if (v.lng != lng) exit(1);    // Ensure conformal addition
  106.      TempVector u (lng);      // Create same size Vector
  107.      for (int i = 0; i < lng; i++) // Carry out addition
  108.           u.ves[i] = ves[i] + v.ves[i];
  109.      return u; }
  110.  
  111.  
  112.